package com.wkbb.basic.controller.open;

import com.wkbb.common.dto.basic.ClassInfo;
import com.wkbb.basic.service.*;
import com.wkbb.common.constants.ErrorCode;
import com.wkbb.common.constants.VersionConstant;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.common.dto.basic.ClassInfoDto;
import com.wkbb.common.dto.basic.GradeTreeDto;
import com.wkbb.common.dto.basic.UserDto;
import com.wkbb.common.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 基础模块对外接口
 */
@RestController
@Api(description = "班级模块对外接口")
@RequestMapping("/basic/{apiVersion}/open")
@Slf4j
public class ClassInfoController {

    @Autowired
    private UserOpenService userOpenService;

    @Autowired
    private ClassInfoService classInfoService;

    @ApiOperation(value = "批量加入班级(非常规班)")
    @RequestMapping(value = "/joinClassByStudentsBack", method = RequestMethod.POST)
    public ResultDTO<Boolean> joinClassByStudentsBack(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion") String apiVersion,
            @ApiParam(value = "学生id(多个,号隔开)", required = true) @RequestParam(name = "studentIds", required = true) String studentIds,
            @ApiParam(value = "班级id", required = true) @RequestParam(name = "classId", required = true) Long classId
    ) {
        log.info("***********ClassInfoManageController.joinClassByStudentsBack******...studentIds:{}，classId:{}", studentIds, classId);
        if (StringUtil.isEmpty(studentIds)) {
            return new ResultDTO<>(ErrorCode.PARAM_MISSING);
        }
        try {
            return classInfoService.joinClassByStudentsBack(studentIds,classId,1);
        } catch (Exception e) {
            log.error("***********ClassInfoManageController.joinClassByStudents******", e);
            return new ResultDTO<>(false);
        }
    }

    @ApiOperation(value = "批量退出班级(非常规班)")
    @RequestMapping(value = "/exitClassByStudentsBack", method = RequestMethod.POST)
    public ResultDTO exitClassByStudentsBack(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion") String apiVersion,
            @ApiParam(value = "学生id(多个,号隔开)", required = true) @RequestParam(name = "studentIds", required = true) String studentIds,
            @ApiParam(value = "班级id", required = true) @RequestParam(name = "classId", required = true) Long classId
    ) {
        log.info("***********ClassInfoManageController.joinClassByStudents******...studentIds:{}，classId:{}", studentIds, classId);
        if (StringUtil.isEmpty(studentIds)) {
            return new ResultDTO<>(ErrorCode.PARAM_MISSING);
        }
        try {
            return classInfoService.joinClassByStudentsBack(studentIds,classId,2);
        } catch (Exception e) {
            log.error("***********ClassInfoManageController.joinClassByStudents******", e);
            return new ResultDTO<>(false);
        }
    }


    @ApiOperation(value = "根据用户获取班级列表接口")
    @RequestMapping(value = "/getClassListByUser", method = RequestMethod.GET)
    public ResultDTO<List<ClassInfoDto>> getClassListByUser(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "组织ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
            @ApiParam(value = "用户ID", required = true) @RequestParam(name = "userId", required = true) Long userId,
            @ApiParam(value = "用户角色", required = true) @RequestParam(name = "roleType", required = true) Integer roleType) {
        log.info("***********ClassInfoController.getClassListByUser******apiVersion:{},orgId:{},userId:{},roleType:{}", apiVersion, orgId, userId, roleType);

        try {

            if (null == orgId || null == userId || null == roleType) {
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }

            List<ClassInfoDto> classList = userOpenService.getClassListByUser(orgId, userId, roleType);

            return new ResultDTO<>(classList);

        } catch (Exception e) {
            log.error("***********ClassInfoController.getClassListByUser******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "根据老师ID查询是否班主任")
    @RequestMapping(value = "/getClassMasterIsTrue", method = RequestMethod.GET)
    public ResultDTO<ClassInfo> getClassMasterIsTrue(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "用户ID", required = true) @RequestParam(name = "userId", required = true) Long userId) {
        log.info("***********ClassInfoController.getClassMasterIsTrue******apiVersion:{},userId:{}", apiVersion, userId);
        try {
            if (null == userId) {
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }
            return new ResultDTO<>(classInfoService.getClassMasterIsTrue(userId));
        } catch (Exception e) {
            log.error("***********ClassInfoController.getClassMasterIsTrue******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "根据老师ID查询是哪个班的班主任")
    @RequestMapping(value = "/getClassInfoByMasterId", method = RequestMethod.GET)
    public ResultDTO<ClassInfoDto> getClassInfoByMasterId(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "组织ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
            @ApiParam(value = "用户ID", required = true) @RequestParam(name = "userId", required = true) Long userId) {
        log.info("***********ClassInfoController.getClassInfoByMasterId******apiVersion:{},userId:{}", apiVersion, userId);
        try {
            if (null == userId) {
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }
            return new ResultDTO<>(classInfoService.getClassInfoByMasterId(orgId,userId));
        } catch (Exception e) {
            log.error("***********ClassInfoController.getClassInfoByMasterId******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "查询班级详情")
    @RequestMapping(value = "/getClassInfo", method = RequestMethod.GET)
    public ResultDTO<ClassInfoDto> getClassInfo(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "组织ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
            @ApiParam(value = "班级ID", required = true) @RequestParam(name = "id", required = true) Long classId) {

        log.info("***********ClassInfoController.getClassInfo******apiVersion:{},orgId:{},classId:{}", apiVersion, orgId, classId);

        try {

            return new ResultDTO<>(userOpenService.getClassInfoDto(orgId, classId));

        } catch (Exception e) {
            log.error("***********ClassInfoController.getClassInfo******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "根据名称查询班级")
    @RequestMapping(value = "/getClassByName", method = RequestMethod.GET)
    public ResultDTO<ClassInfoDto> getClassByName(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "组织ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
            @ApiParam(value = "年级ID", required = true) @RequestParam(name = "gradeId", required = true) Long gradeId,
            @ApiParam(value = "班级名称", required = true) @RequestParam(name = "className", required = true) String className) {

        log.info("***********ClassInfoController.getClassInfo******apiVersion:{},orgId:{},className:{}", apiVersion, orgId, className);

        try {

            Map<String, Object> params = new HashMap<>(3);
            params.put("orgId", orgId);
            params.put("gradeId", gradeId);
            params.put("className", className);

            return new ResultDTO<ClassInfoDto>(userOpenService.getClassInfo(params));

        } catch (Exception e) {
            log.error("***********ClassInfoController.getClassInfo******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "获取班级年级树形结构")
    @RequestMapping(value = "/getClassTree", method = RequestMethod.GET)
    public ResultDTO<List<GradeTreeDto>> getClassTree(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "组织ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId) {
        log.info("***********ClassInfoController.getClassTree******apiVersion:{},orgId:{}", apiVersion, orgId);
        try {
            return new ResultDTO<>(userOpenService.getClassTree(orgId));
        } catch (Exception e) {
            log.error("***********ClassInfoController.getClassTree******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "查询全校班级列表(不分页)")
    @RequestMapping(value = "getClassListByOrg", method = RequestMethod.GET)
    public ResultDTO<List<ClassInfo>> getClassListByOrg(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "学校ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId) {
        log.info("***********ClassInfoController.list******apiVersion:{},orgId:{},gradeId:{},className:{},masterName:{}",
                apiVersion, orgId);
        try {
            if (orgId == 0) {
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }

            Map<String, Object> params = new HashMap<>(2);
            params.put("orgId", orgId);
            params.put("status", 1);

            return new ResultDTO<>(classInfoService.getListByMapParams(params));
        } catch (Exception e) {
            log.error("***********ClassInfoController.list******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    /**
     * 临时方法,此方法在上线之后会被废弃
     * @param apiVersion
     * @param oldClassIds
     * @return
     */
    @ApiOperation(value = "批量查询2.0版本学校ID")
    @RequestMapping(value = "getNewClassListByOldIds", method = RequestMethod.GET)
    public ResultDTO<List<ClassInfoDto>> getNewClassListByOldIds(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "班级IDs", required = true) @RequestParam(name = "oldClassIds", required = true) List<Long> oldClassIds) {

        log.info("***********OrganizationController.getNewClassListByOldIds******apiVersion:{},oldOrgIds:{}", apiVersion, oldClassIds);

        try {
            return new ResultDTO<List<ClassInfoDto>>(classInfoService.getNewClassListByOldIds(oldClassIds));
        } catch (Exception e) {
            log.error("***********OrganizationController.getNewClassListByOldIds******", e);
            return new ResultDTO<List<ClassInfoDto>>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "根据orgId清除班级数据接口")
    @PostMapping(value = "deleteClassData")
    public ResultDTO deleteClassData(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "学校ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId){
        try{
            //在此验证必传参数
            if(null==orgId){
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }
            classInfoService.deleteByOrgId(orgId);
            return new ResultDTO();
        }catch (Exception e){
            log.error("*************ClassInfoController.deleteClassData*************异常:{}", e);
            return new ResultDTO(false);
        }
    }

    @ApiOperation(value = "查询全校班级列表(对外接口不分页)")
    @RequestMapping(value = "getClassInfoListByOrg", method = RequestMethod.GET)
    public ResultDTO<List<ClassInfoDto>> getClassInfoListByOrg(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "学校ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId) {
        log.info("***********ClassInfoController.getClassInfoListByOrg******orgId:{}", orgId);
        try {
            if (orgId == 0) {
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }

            List<ClassInfoDto> classInfoDtoList  = new ArrayList<>();

            Map<String, Object> params = new HashMap<>(2);
            params.put("orgId", orgId);
            params.put("status", 1);

            List<ClassInfo> classList = classInfoService.getListByMapParams(params);
            for(ClassInfo classInfo : classList)
            {
                ClassInfoDto classInfoDto = new ClassInfoDto();
                BeanUtils.copyProperties(classInfo, classInfoDto);
                classInfoDtoList.add(classInfoDto);
            }
            return new ResultDTO<>(classInfoDtoList);
        } catch (Exception e) {
            log.error("***********ClassInfoController.getClassInfoListByOrg******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "查询全校班级列表(对外接口不分页)")
    @RequestMapping(value = "getOrgClassList", method = RequestMethod.GET)
    public ResultDTO<List<ClassInfoDto>> getOrgClassList(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "学校ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
            @ApiParam(value = "年级ID", required = true) @RequestParam(name = "gradeId", required = true) Long gradeId,
            @ApiParam(value = "是否为常规班") @RequestParam(name = "isRoutine", required = false) Boolean isRoutine) {
        log.info("***********ClassInfoController.getClassInfoListByOrg******orgId:{}", orgId);
        try {
            if (orgId == 0) {
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }

            List<ClassInfoDto> classInfoDtoList  = new ArrayList<>();

            Map<String, Object> params = new HashMap<>(2);
            params.put("orgId", orgId);
            params.put("gradeId", gradeId);
            params.put("isRoutine", isRoutine);
            List<ClassInfo> classList = classInfoService.getListByMapParams(params);
            for(ClassInfo classInfo : classList)
            {
                ClassInfoDto classInfoDto = new ClassInfoDto();
                BeanUtils.copyProperties(classInfo, classInfoDto);
                classInfoDtoList.add(classInfoDto);
            }
            return new ResultDTO<>(classInfoDtoList);
        } catch (Exception e) {
            log.error("***********ClassInfoController.getClassInfoListByOrg******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

}
